#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<string.h>
#include<stdlib.h>
#define NUM 128
#define CMD_NUM 64
int main()
{
  char command[NUM];
  char* argv[CMD_NUM] = {NULL};
  for(;;)
  {
    command[0]= 0;
    //1.打印提示符
    printf("[you-know-who@myhostname mydir]$");
    fflush(stdout);
    
    //2.获取提示符
    fgets(command,NUM,stdin);
    command[strlen(command)-1]= 0;

    //3.解析命令行字符串 
    const char* sep = "";
    argv[0] = strtok(command,sep);
    int i = 1;
    while(argv[i] = strtok(NULL,sep))
    {
      i++;
    }

    if(strcmp(argv[0],"cd") == 0)
    {
      if(argv[1])
      {
        chdir(argv[1]);
      }
      continue;
    } 
    //执行第三方命令
    if(fork() == 0)
    {
      //child
      execvp(argv[0],argv);
      exit(1);
    }

    //parent
    waitpid(-1,NULL,0);
  }
  return 0;
}
